﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MuMen
{
    public partial class Form1 : Form
    {
        #region 变量

        List<DingDanContent> dingDanContents = new List<DingDanContent>();

        List<DingDan> dingDans = new List<DingDan>();

        List<float> heights = new List<float>();
        List<int> randomInts = new List<int>();
        List<float> huaDaos = new List<float>();

        DingDanContent dingDanContentSelected = new DingDanContent();

        DingDan dingDanSelected = new DingDan();

        //排序编号
        int orderBy = 0;

        #endregion

        #region 控件事件

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            dingDans = DingDan.GetChooser();

            LoadDingDanDataListView(dingDans, 1);


            dingDanContents = DingDanContent.GetChooser();


            LoadDingDanContentDataListView(dingDanContents, 1,true);
            //ListView.Column[0].Width := -1;//列宽根据列内容自适应，此时保证列内容都可见。

            //2、设置ListView.Column[0].Width := -2;//列宽根据列标题自适应，此时保证列标题可见。
            //lvDingDanData.Columns[2].Width = -1;
            //lvDingDanData.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); // 自动调整列宽。
            //this.ClientSize = new Size(columnHeaderTemp.Width, ClientSize.Height);
        }

        //订单新增
        private void btnDingDanOK_Click(object sender, EventArgs e)
        {
            DingDan dingDan = new DingDan();
            dingDan.DingDanNo = tbDingDanNo.Text;
            dingDan.DingDanPi = tbPi.Text;
            dingDan.DingDanColor = tbColor.Text;
            dingDan.TaoXianCount = 0;
            dingDan.TaoBanCount = 0;
            dingDan.CustomName = tbCustomName.Text;
            dingDan.CustomTelephone = tbCustomTel.Text;
            dingDan.DingDanComeDate = dtDing.Value.Date;
            dingDan.DingDanGoDate = dtJiao.Value.Date;

            if (dingDans == null)
            {
                dingDans = new List<DingDan>();
            }

            dingDans.Add(dingDan);

            DingDan.SetChooser(dingDans);

            LoadDingDanDataListView(dingDans, 1);

            //读取内容
            dingDanSelected = dingDan;
            LoadDingDanContentDataListView(dingDanContents, 1, true);
        }
        //订单修改
        private void button10_Click(object sender, EventArgs e)
        {
            foreach (DingDan _dingDan in dingDans)
            {
                if (_dingDan.DingDanID == dingDanSelected.DingDanID)
                {
                    _dingDan.DingDanNo = tbDingDanNo.Text;
                    _dingDan.DingDanPi = tbPi.Text;
                    _dingDan.DingDanColor = tbColor.Text;
                    _dingDan.CustomName = tbCustomName.Text;
                    _dingDan.CustomTelephone = tbCustomTel.Text;
                    _dingDan.DingDanComeDate = dtDing.Value.Date;
                    _dingDan.DingDanGoDate = dtJiao.Value.Date;
                    DingDan.SetChooser(dingDans);

                    LoadDingDanDataListView(dingDans, 1);
                }
            }
        }
        //订单删除
        private void button11_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.DialogResult dr= MessageBox.Show("确定要删除吗？", "删除", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes & lvDingDanData.SelectedItems.Count > 0)
            {
                DingDan dingDan = (DingDan)lvDingDanData.Items[lvDingDanData.SelectedItems[0].SubItems[0].Text].Tag;

                foreach (DingDan _dingDan in dingDans)
                {
                    if (_dingDan.DingDanID == dingDan.DingDanID)
                    {
                        dingDans.Remove(_dingDan);
                        break;
                    }
                }

                DingDan.SetChooser(dingDans);

                LoadDingDanDataListView(dingDans, 1);
            }
            else
            {
                LoadDingDanDataListView(dingDans, 1);
            }
        }
        
        //订单内容增加
        private void button2_Click(object sender, EventArgs e)
        {
            DingDanContent dingDanContent = new DingDanContent();
            dingDanContent.DingDanContentID = Guid.NewGuid().ToString();
            dingDanContent.DingDanID = dingDanSelected.DingDanID;
            dingDanContent.DingDanContentType = cbDingDanType.Text;
            dingDanContent.DingDanContentXingHao = cbXingHao.Text;
            dingDanContent.DingDanContentCaiZhong = cbCaiZhong.Text;
            dingDanContent.DingDanContentName = tbDingDanContentName.Text;

            //dingDanContent.DingDanContentLength = Convert.ToInt32(tbLength.Text);
            //dingDanContent.DingDanContentWeight = Convert.ToInt32(tbHeight.Text);
            //dingDanContent.DingDanContentCount = Convert.ToInt32(tbCount.Text);
            dingDanContent.PassHeight = Convert.ToInt32(tbLength.Text);
            dingDanContent.PassWidth = Convert.ToInt32(tbHeight.Text);
            dingDanContent.PassThickness = Convert.ToInt32(tbCount.Text);

            if (dingDanContent.PassHeight == 0)
            {
                dingDanContent.DingDanContentLength = 0;
                dingDanContent.DingDanContentWeight = 0;
                dingDanContent.DingDanContentCount = 0;

                dingDanContent.DingDanContentLength1 = 0;
                dingDanContent.DingDanContentWeight1 = 0;
                dingDanContent.DingDanContentCount1 = 0;

            }
            else
            {
                dingDanContent.DingDanContentLength = dingDanContent.PassHeight;
                dingDanContent.DingDanContentWeight = dingDanContent.PassThickness - 5;
                dingDanContent.DingDanContentCount = 2;

                dingDanContent.DingDanContentLength1 = dingDanContent.PassWidth;
                dingDanContent.DingDanContentWeight1 = dingDanContent.PassThickness - 5;
                dingDanContent.DingDanContentCount1 = 1;

            }

            
            //////////////////////////////////////////////////////////////////////////////////////
            if (dingDanContent.DingDanContentLength > 0)
            {
                float mod = dingDanContent.DingDanContentLength % 100;

                if (mod == 0)
                {
                    dingDanContent.TaoXianLength = dingDanContent.DingDanContentLength + 100;
                }
                else
                {
                    if (mod <= 50)
                    {
                        dingDanContent.TaoXianLength = dingDanContent.DingDanContentLength + 150 - mod;
                    }
                    else
                    {
                        dingDanContent.TaoXianLength = dingDanContent.DingDanContentLength + 200 - mod;
                    }
                }

                if (cbCaiZhong.Text == "双开")
                {
                    dingDanContent.TaoXianCount = dingDanContent.DingDanContentCount * 2;
                }
                else if (cbCaiZhong.Text == "单开")
                {
                    dingDanContent.TaoXianCount = dingDanContent.DingDanContentCount;
                }
                else
                {
                    dingDanContent.TaoXianCount = 0;
                    dingDanContent.TaoXianLength = 0;
                }    
            }
            else
            {
                dingDanContent.TaoXianCount = 0;
                dingDanContent.TaoXianLength = 0;
            }

            //////////////////////////////////////////////////////////////////////////////////////////
            if (dingDanContent.DingDanContentLength1 > 0)
            {
                float mod1 = dingDanContent.DingDanContentLength1%100;

                if (mod1 == 0)
                {
                    dingDanContent.TaoXianLength1 = dingDanContent.DingDanContentLength1 + 100;
                }
                else
                {
                    if (mod1 <= 50)
                    {
                        dingDanContent.TaoXianLength1 = dingDanContent.DingDanContentLength1 + 150 - mod1;
                    }
                    else
                    {
                        dingDanContent.TaoXianLength1 = dingDanContent.DingDanContentLength1 + 200 - mod1;
                    }
                }

                if (cbCaiZhong.Text == "双开")
                {
                    dingDanContent.TaoXianCount1 = dingDanContent.DingDanContentCount1*2;
                }
                else if (cbCaiZhong.Text == "单开")
                {
                    dingDanContent.TaoXianCount1 = dingDanContent.DingDanContentCount1;
                }
                else
                {
                    dingDanContent.TaoXianCount1 = 0;
                    dingDanContent.TaoXianLength1 = 0;
                }
            }
            else
            {
                dingDanContent.TaoXianCount1 = 0;
                dingDanContent.TaoXianLength1 = 0;
            }
            //////////////////////////////////////////////////////////////////////////////////////////
            if (dingDanContents == null)
            { 
                dingDanContents=new List<DingDanContent>();
            }

            dingDanContent.CreateTime = DateTime.Now;

            if (dingDanContent.DingDanContentType == "钛镁合金推拉门")
            {
                //边高：高-35
                float bianGao=dingDanContent.DingDanContentLength-35;
                // 码头： 宽度-边数（个数+1）*50     /个数
                float maTou=(dingDanContent.DingDanContentWeight - (dingDanContent.DingDanContentCount+1) * 50) / dingDanContent.DingDanContentCount;

                if (dingDanContent.DingDanContentCount == 4)
                {
                    maTou = (dingDanContent.DingDanContentWeight - (dingDanContent.DingDanContentCount + 2) * 50) / dingDanContent.DingDanContentCount;

                }

                dingDanContent.BeiZhu = string.Format("推拉门 尺寸：{0}×{1} 每扇：{2}×{3} 玻璃：{4}×{5} 共{6}扇 {7}",
                    dingDanContent.DingDanContentLength,dingDanContent.DingDanContentWeight,
                    
                    bianGao,maTou,
                    //玻璃   边高-64       码头+15
                    bianGao-64,maTou+15,
                    dingDanContent.DingDanContentCount,
                dingDanContent.DingDanContentCount == 4 ? "有对开" : ""
                    );
            }
            else if (dingDanContent.DingDanContentType == "钛镁合金门")
            {
                //边高：高-(37+10=47)
                float bianGao = dingDanContent.DingDanContentLength - 47 - 5;
                // 码头： 宽度-(37*2+7=81)
                float maTou = dingDanContent.DingDanContentWeight - 81 - 5;

                dingDanContent.BeiZhu = string.Format("门 尺寸：{0}×{1} 去缝：{2}×{3} 做门：{4}×{5} 玻璃：{6}×{7}",
                    dingDanContent.DingDanContentLength, dingDanContent.DingDanContentWeight,
                    dingDanContent.DingDanContentLength - 5, dingDanContent.DingDanContentWeight - 5,
                    bianGao, maTou,
                    //玻璃 去掉门边 73*2+5
                    bianGao - 151, maTou - 151
                    );
            }
            else if(dingDanContent.DingDanContentType == "门套板")
            {
                if (dingDanContent.DingDanContentLength>0)
                {
                    dingDanContent.BeiZhu = string.Format("{0}*{1}门板",
                        dingDanContent.PassHeight-40,
                        dingDanContent.PassWidth-65);
                    
                }
                else
                {
                    dingDanContent.BeiZhu = "";                    
                }
            }
            else
            {
                dingDanContent.BeiZhu = "";
            }

            dingDanContents.Add(dingDanContent);

            DingDanContent.SetChooser(dingDanContents);

            LoadDingDanContentDataListView(dingDanContents, 1,true);

            tbDingDanContentName.Text = "";
            tbCount.Text = "0";
            tbHeight.Text = "0";
            tbLength.Text = "0";

            //if (dingDanContent.DingDanContentCount > 1)
            //{

            //    tbCount.Text = (dingDanContent.DingDanContentCount / 2).ToString();
            //}
            //else if (dingDanContent.DingDanContentCount == 1)
            //{
            //    tbCount.Text = (dingDanContent.DingDanContentCount * 2).ToString();
            //}

            //tbLength.Text = "";
            //tbHeight.Text = dingDanContent.DingDanContentWeight.ToString();
            tbDingDanContentName.Select();
        }

        //修改
        private void button3_Click(object sender, EventArgs e)
        {
            foreach (DingDanContent _dingDanContent in dingDanContents)
            {
                if (_dingDanContent.DingDanContentID == dingDanContentSelected.DingDanContentID)
                { 
                    //修改

                    _dingDanContent.DingDanContentType = cbDingDanType.Text;
                    _dingDanContent.DingDanContentXingHao = cbXingHao.Text;
                    _dingDanContent.DingDanContentCaiZhong = cbCaiZhong.Text;
                    _dingDanContent.PassHeight = Convert.ToInt32(tbLength.Text);
                    _dingDanContent.PassWidth = Convert.ToInt32(tbHeight.Text);
                    _dingDanContent.PassThickness = Convert.ToInt32(tbCount.Text);
                    _dingDanContent.DingDanContentName = tbDingDanContentName.Text;

                    if (_dingDanContent.PassHeight == 0)
                    {
                        _dingDanContent.DingDanContentLength = 0;
                        _dingDanContent.DingDanContentWeight = 0;
                        _dingDanContent.DingDanContentCount = 0;

                        _dingDanContent.DingDanContentLength1 = 0;
                        _dingDanContent.DingDanContentWeight1 = 0;
                        _dingDanContent.DingDanContentCount1 = 0;

                    }
                    else
                    {
                        _dingDanContent.DingDanContentLength = _dingDanContent.PassHeight;
                        _dingDanContent.DingDanContentWeight = _dingDanContent.PassThickness - 5;
                        _dingDanContent.DingDanContentCount = 2;

                        _dingDanContent.DingDanContentLength1 = _dingDanContent.PassWidth;
                        _dingDanContent.DingDanContentWeight1 = _dingDanContent.PassThickness - 5;
                        _dingDanContent.DingDanContentCount1 = 1;
                    }

                    if (_dingDanContent.DingDanContentLength > 0)
                    {
                        float mod = _dingDanContent.DingDanContentLength%100;

                        if (mod == 0)
                        {
                            _dingDanContent.TaoXianLength = _dingDanContent.DingDanContentLength + 100;
                        }
                        else
                        {
                            if (mod <= 50)
                            {
                                _dingDanContent.TaoXianLength = _dingDanContent.DingDanContentLength + 150 - mod;
                            }
                            else
                            {
                                _dingDanContent.TaoXianLength = _dingDanContent.DingDanContentLength + 200 - mod;
                            }
                        }

                        if (cbCaiZhong.Text == "双开")
                        {
                            _dingDanContent.TaoXianCount = _dingDanContent.DingDanContentCount*2;
                        }
                        else if (cbCaiZhong.Text == "单开")
                        {
                            _dingDanContent.TaoXianCount = _dingDanContent.DingDanContentCount;
                        }
                        else
                        {
                            _dingDanContent.TaoXianCount = 0;
                            _dingDanContent.TaoXianLength = 0;
                        }
                    }
                    else
                    {
                        _dingDanContent.TaoXianCount = 0;
                        _dingDanContent.TaoXianLength = 0;
                    }
                    //////////////////////////////////////////////////////////////////////////////////////////
                    if (_dingDanContent.DingDanContentLength1 > 0)
                    {
                        float mod1 = _dingDanContent.DingDanContentLength1%100;

                        if (mod1 == 0)
                        {
                            _dingDanContent.TaoXianLength1 = _dingDanContent.DingDanContentLength1 + 100;
                        }
                        else
                        {
                            if (mod1 <= 50)
                            {
                                _dingDanContent.TaoXianLength1 = _dingDanContent.DingDanContentLength1 + 150 - mod1;
                            }
                            else
                            {
                                _dingDanContent.TaoXianLength1 = _dingDanContent.DingDanContentLength1 + 200 - mod1;
                            }
                        }

                        if (cbCaiZhong.Text == "双开")
                        {
                            _dingDanContent.TaoXianCount1 = _dingDanContent.DingDanContentCount1*2;
                        }
                        else if (cbCaiZhong.Text == "单开")
                        {
                            _dingDanContent.TaoXianCount1 = _dingDanContent.DingDanContentCount1;
                        }
                        else
                        {
                            _dingDanContent.TaoXianCount1 = 0;
                            _dingDanContent.TaoXianLength1 = 0;
                        }
                    }
                    else
                    {
                        _dingDanContent.TaoXianCount1 = 0;
                        _dingDanContent.TaoXianLength1 = 0;
                    }
                    //////////////////////////////////////////////////////////////////////////////////////////
                    if (_dingDanContent.DingDanContentType == "门套板")
                    {
                        if (_dingDanContent.DingDanContentLength > 0)
                        {
                            _dingDanContent.BeiZhu = string.Format("{0}*{1}门板",
                                _dingDanContent.PassHeight - 40,
                                _dingDanContent.PassWidth - 65);

                        }
                    }

                    //dingDanContents.Add(_dingDanContent);

                    DingDanContent.SetChooser(dingDanContents);

                    LoadDingDanContentDataListView(dingDanContents, 1,true);

                    
                }
            }
            tbDingDanContentName.Select();
        }

        //直接修改
        private void button6_Click(object sender, EventArgs e)
        {
            foreach (DingDanContent _dingDanContent in dingDanContents)
            {
                if (_dingDanContent.DingDanContentID == dingDanContentSelected.DingDanContentID)
                { 
                    //修改

                    _dingDanContent.DingDanContentType = cbDingDanType.Text;
                    _dingDanContent.DingDanContentXingHao = cbXingHao.Text;
                    _dingDanContent.DingDanContentCaiZhong = cbCaiZhong.Text;
                    //_dingDanContent.PassHeight = Convert.ToInt32(tbLength.Text);
                    //_dingDanContent.PassWidth = Convert.ToInt32(tbHeight.Text);
                    //_dingDanContent.PassThickness = Convert.ToInt32(tbCount.Text);
                    _dingDanContent.DingDanContentName = tbDingDanContentName.Text;

                    
                    //_dingDanContent.DingDanContentLength1 = _dingDanContent.PassWidth;
                    //_dingDanContent.DingDanContentWeight1 = _dingDanContent.PassThickness - 5;
                    //_dingDanContent.DingDanContentCount1 = 1;

                    if (cbTaoBanDiff.Text == "第一套板")
                    {
                        _dingDanContent.DingDanContentLength = Convert.ToInt32(tbLength.Text);
                        _dingDanContent.DingDanContentWeight = Convert.ToInt32(tbHeight.Text);
                        _dingDanContent.DingDanContentCount = Convert.ToInt32(tbCount.Text);

                        if (_dingDanContent.DingDanContentLength > 0)
                        {
                            float mod = _dingDanContent.DingDanContentLength%100;

                            if (mod == 0)
                            {
                                _dingDanContent.TaoXianLength = _dingDanContent.DingDanContentLength + 100;
                            }
                            else
                            {
                                if (mod <= 50)
                                {
                                    _dingDanContent.TaoXianLength = _dingDanContent.DingDanContentLength + 150 - mod;
                                }
                                else
                                {
                                    _dingDanContent.TaoXianLength = _dingDanContent.DingDanContentLength + 200 - mod;
                                }
                            }

                            if (cbCaiZhong.Text == "双开")
                            {
                                _dingDanContent.TaoXianCount = _dingDanContent.DingDanContentCount*2;
                            }
                            else if (cbCaiZhong.Text == "单开")
                            {
                                _dingDanContent.TaoXianCount = _dingDanContent.DingDanContentCount;
                            }
                            else
                            {
                                _dingDanContent.TaoXianCount = 0;
                                _dingDanContent.TaoXianLength = 0;
                            }
                        }
                        else
                        {
                            _dingDanContent.TaoXianCount = 0;
                            _dingDanContent.TaoXianLength = 0;
                        }
                        cbTaoBanDiff.Text = "第二套板";
                    }
                        //////////////////////////////////////////////////////////////////////////////////////////

                    else if (cbTaoBanDiff.Text == "第二套板")
                    {
                        _dingDanContent.DingDanContentLength1 = Convert.ToInt32(tbLength.Text);
                        _dingDanContent.DingDanContentWeight1 = Convert.ToInt32(tbHeight.Text);
                        _dingDanContent.DingDanContentCount1 = Convert.ToInt32(tbCount.Text);

                        if (_dingDanContent.DingDanContentLength1 > 0)
                        {
                            float mod1 = _dingDanContent.DingDanContentLength1%100;

                            if (mod1 == 0)
                            {
                                _dingDanContent.TaoXianLength1 = _dingDanContent.DingDanContentLength1 + 100;
                            }
                            else
                            {
                                if (mod1 <= 50)
                                {
                                    _dingDanContent.TaoXianLength1 = _dingDanContent.DingDanContentLength1 + 150 - mod1;
                                }
                                else
                                {
                                    _dingDanContent.TaoXianLength1 = _dingDanContent.DingDanContentLength1 + 200 - mod1;
                                }
                            }

                            if (cbCaiZhong.Text == "双开")
                            {
                                _dingDanContent.TaoXianCount1 = _dingDanContent.DingDanContentCount1*2;
                            }
                            else if (cbCaiZhong.Text == "单开")
                            {
                                _dingDanContent.TaoXianCount1 = _dingDanContent.DingDanContentCount1;
                            }
                            else
                            {
                                _dingDanContent.TaoXianCount1 = 0;
                                _dingDanContent.TaoXianLength1 = 0;
                            }
                        }
                        else
                        {
                            _dingDanContent.TaoXianCount1 = 0;
                            _dingDanContent.TaoXianLength1 = 0;
                        }
                        cbTaoBanDiff.Text = "第一套板";
                    }
                    //////////////////////////////////////////////////////////////////////////////////////////
            
                    //dingDanContents.Add(_dingDanContent);

                    if (_dingDanContent.DingDanContentType == "钛镁合金推拉门")
                    {
                        //边高：高-35
                        float bianGao = _dingDanContent.DingDanContentLength - 35;
                        // 码头： 宽度-边数（个数+1）*50     /个数
                        float maTou = (_dingDanContent.DingDanContentWeight - (_dingDanContent.DingDanContentCount + 1) * 50) / _dingDanContent.DingDanContentCount;

                        if (_dingDanContent.DingDanContentCount == 4)
                        {
                            maTou = (_dingDanContent.DingDanContentWeight - (_dingDanContent.DingDanContentCount + 2) * 50) / _dingDanContent.DingDanContentCount;

                        }

                        _dingDanContent.BeiZhu = string.Format("推拉门 尺寸：{0}×{1} 每扇：{2}×{3} 玻璃：{4}×{5} 共{6}扇 {7}",
                            _dingDanContent.DingDanContentLength, _dingDanContent.DingDanContentWeight,

                            bianGao, maTou,
                            //玻璃   边高-64       码头+15
                            bianGao - 64, maTou + 15,
                            _dingDanContent.DingDanContentCount,
                        _dingDanContent.DingDanContentCount == 4 ? "有对开" : ""
                            );
                    }

                    DingDanContent.SetChooser(dingDanContents);

                    LoadDingDanContentDataListView(dingDanContents, 1,true);

                    
                }
            }
            tbDingDanContentName.Select();
        }

        //删除
        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.DialogResult dr = MessageBox.Show("确定要删除吗？", "删除", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes & lvDingdanContentData.SelectedItems.Count > 0)
            {
                DingDanContent dingDanContent =
                    (DingDanContent)
                        lvDingdanContentData.Items[lvDingdanContentData.SelectedItems[0].SubItems[0].Text].Tag;

                foreach (DingDanContent _dingDanContent in dingDanContents)
                {
                    if (_dingDanContent.DingDanContentID == dingDanContent.DingDanContentID)
                    {
                        dingDanContents.Remove(_dingDanContent);
                        break;
                    }
                }

                DingDanContent.SetChooser(dingDanContents);

                LoadDingDanContentDataListView(dingDanContents, 1, true);
            }
        }

        //计算
        private void button4_Click(object sender, EventArgs e)
        {
            foreach (DingDan dingDan in dingDans)
            { 
                if(dingDan.DingDanID == dingDanSelected.DingDanID)
                {
                    int taoXianCount=0,taoBanCount=0,menCount=0 ;
                    float qianSub = 0, taoXianMiSub = 0, taoBanMiSub = 0, chuangTaoBanMiShu = 0;
                    double chuangTaoBanPing = 0,menTaoBanPing=0;

                    float chuangTaoBanDanMiShu = 0, chuangTaoBanShuangMiShu = 0;
                    int chuangTaoBanDanShu = 0, chuangTaoBanShuangShu = 0;
                    List<TaoXianContent> taoxianContents = new List<TaoXianContent>();

                    foreach (DingDanContent dingDanContent in dingDanContents)
                    {
                        if (dingDanContent.DingDanID == dingDanSelected.DingDanID)
                        {
                            //套线
                            if (dingDanContent.TaoXianCount > 0)
                            {
                                TaoXianContent taoXianContent=new TaoXianContent();
                                taoXianContent.TaoXianCount = dingDanContent.TaoXianCount;
                                taoXianContent.TaoXianLength = dingDanContent.TaoXianLength;
                                taoxianContents.Add(taoXianContent);
                            }
                            if (dingDanContent.TaoXianCount1 > 0)
                            {
                                TaoXianContent taoXianContent = new TaoXianContent();
                                taoXianContent.TaoXianCount = dingDanContent.TaoXianCount1;
                                taoXianContent.TaoXianLength = dingDanContent.TaoXianLength1;
                                taoxianContents.Add(taoXianContent);
                            }

                            if (dingDanContent.DingDanContentType == "门")
                            {
                                menCount += dingDanContent.DingDanContentCount;
                                menCount += dingDanContent.DingDanContentCount1;
                            }
                            else
                            {
                                taoBanCount += dingDanContent.DingDanContentCount;
                                taoBanCount += dingDanContent.DingDanContentCount1;

                                taoXianCount += dingDanContent.TaoXianCount;
                                taoXianCount += dingDanContent.TaoXianCount1;

                                taoXianMiSub += dingDanContent.TaoXianLength * dingDanContent.TaoXianCount;
                                taoXianMiSub += dingDanContent.TaoXianLength1 * dingDanContent.TaoXianCount1;

                                taoBanMiSub += dingDanContent.DingDanContentLength * dingDanContent.DingDanContentCount;
                                taoBanMiSub += dingDanContent.DingDanContentLength1 * dingDanContent.DingDanContentCount1;

                                dingDanContent.TaoBanPing =  (dingDanContent.DingDanContentLength
                                    * dingDanContent.DingDanContentCount
                                    * dingDanContent.DingDanContentWeight
                                    +dingDanContent.TaoXianLength * dingDanContent.TaoXianCount * 100) / 1000000;

                                dingDanContent.TaoBanPing += (dingDanContent.DingDanContentLength1
                                        * dingDanContent.DingDanContentCount1
                                        * dingDanContent.DingDanContentWeight1
                                        + dingDanContent.TaoXianLength1 * dingDanContent.TaoXianCount1 * 100) / 1000000;
                                    
                                if (dingDanContent.DingDanContentType == "窗套板")
                                {
                                    chuangTaoBanMiShu += dingDanContent.DingDanContentLength * dingDanContent.DingDanContentCount;
                                    chuangTaoBanMiShu += dingDanContent.DingDanContentLength1 * dingDanContent.DingDanContentCount1;
  
                                    chuangTaoBanPing += dingDanContent.TaoBanPing;

                                    if(dingDanContent.DingDanContentCaiZhong =="单开")
                                    {
                                        chuangTaoBanDanMiShu += dingDanContent.DingDanContentLength * dingDanContent.DingDanContentCount;
                                        chuangTaoBanDanMiShu += dingDanContent.DingDanContentLength1 * dingDanContent.DingDanContentCount1;

                                        if (dingDanContent.DingDanContentWeight >= 500)
                                        {
                                            chuangTaoBanShuangShu += dingDanContent.DingDanContentCount;
                                            
                                        }
                                        else
                                        {
                                            chuangTaoBanDanShu += dingDanContent.DingDanContentCount;
                                        }

                                        if (dingDanContent.DingDanContentWeight1 >= 500)
                                        {
                                            chuangTaoBanShuangShu += dingDanContent.DingDanContentCount1;

                                        }
                                        else
                                        {
                                            chuangTaoBanDanShu += dingDanContent.DingDanContentCount1;
                                        }
                                        

                                    }
                                    else if (dingDanContent.DingDanContentCaiZhong == "双开")
                                    {
                                        chuangTaoBanShuangMiShu += dingDanContent.DingDanContentLength * dingDanContent.DingDanContentCount;
                                        chuangTaoBanShuangMiShu += dingDanContent.DingDanContentLength1 * dingDanContent.DingDanContentCount1;

                                        chuangTaoBanShuangShu += dingDanContent.DingDanContentCount;
                                        chuangTaoBanShuangShu += dingDanContent.DingDanContentCount1;
                                    }
                                }
                                else if (dingDanContent.DingDanContentType == "门套板")
                                {
                                    menTaoBanPing += dingDanContent.TaoBanPing;
                                }
                            }
                        }
                        //QianSub
                    }

                    taoxianContents = taoxianContents.OrderBy(o => o.TaoXianLength).ToList();
                    
                    //挑选相同的
                    TaoXianContent shangXianContent = taoxianContents[0];
                    int i = 0;
                    foreach (TaoXianContent xianContent in taoxianContents)
                    {
                        if (i==0)
                        {
                            i++;
                            continue;
                        }

                        if (shangXianContent.TaoXianCount>0)
                        {
                            if (shangXianContent.TaoXianLength == xianContent.TaoXianLength)
                            {
                                xianContent.TaoXianCount += shangXianContent.TaoXianCount;
                                taoxianContents[i-1].TaoXianCount = 0;
                                taoxianContents[i-1].TaoXianLength = 0;
                                
                            }
                        }
                        i++;
                        shangXianContent = xianContent;

                    }
                    dingDan.TaoXianSuan = "";
                    foreach (TaoXianContent xianContent in taoxianContents)
                    {
                        if (xianContent.TaoXianCount>0)
                        {
                            dingDan.TaoXianSuan += string.Format("{0}={1}  ",
                           xianContent.TaoXianLength/1000, xianContent.TaoXianCount);
                        }
                    }
                    dingDan.TaoBanCount = taoBanCount;
                    dingDan.TaoXianCount = taoXianCount;
                    dingDan.MenCount = menCount;
                    dingDan.TaoXianMiSub = taoXianMiSub / 1000;
                    dingDan.TaoBanMiSub = taoBanMiSub / 1000;
                    dingDan.TaoBanDanCount = chuangTaoBanDanShu;
                    dingDan.TaoBanShuangCount = chuangTaoBanShuangShu;

                    dingDan.QianSub = chuangTaoBanPing; //string.Format("窗套总长度：{0},总平方：{1}", chuangTaoBanMiShu / 1000,chuangTaoBanPing/1000000);

                    if (dingDan.DingDanPi == "木皮")
                    {
                        //贴皮 40 30
                        dingDan.TiePiFeiStr = String.Format("贴皮：门 {0}*40={1} 双 {2}*40/6={3} 单 {4}*40/9={5} 合计:{6}"
                            ,menCount,menCount * 40.0
                            ,chuangTaoBanShuangShu,(chuangTaoBanShuangShu*(40.0/6)).ToString("f2")
                            ,chuangTaoBanDanShu,(chuangTaoBanDanShu*(40.0/9)).ToString("f2")
                            ,(menCount * 40.0+chuangTaoBanShuangShu*(40.0/6)+chuangTaoBanDanShu*(40.0/9)).ToString("f2"));
                        //喷漆 115 90
                        dingDan.PenQiFeiStr = String.Format("油工：门 {0}*115={1} 双 {2}*115/6={3} 单 {4}*115/9={5}合计:{6}"
                            , menCount, menCount * 115.0
                            , chuangTaoBanShuangShu, (chuangTaoBanShuangShu * (115.0 / 6)).ToString("f2")
                            , chuangTaoBanDanShu, (chuangTaoBanDanShu * (115.0 / 9)).ToString("f2")
                            , (menCount * 115.0 + chuangTaoBanShuangShu * (115.0 / 6) + chuangTaoBanDanShu * (115.0 / 9)).ToString("f2"));



                        //喷漆 70
                        dingDan.BeiZhu = string.Format("喷漆每平70,套板平方:{0},喷窗套漆钱:{1},喷门{4},套板卖钱:{2},套板每平36.5料,料钱:{3}"
                            ,chuangTaoBanPing+menTaoBanPing,chuangTaoBanPing * 70,
                            chuangTaoBanDanMiShu * 50 / 1000 + chuangTaoBanShuangMiShu * 70 / 1000,
                            (menTaoBanPing+ chuangTaoBanPing) * 36.5,
                            menCount * 280);

                        //50  70
                    }
                    else if (dingDan.DingDanPi == "纸皮")
                    {
                        //贴皮 40 30
                        dingDan.TiePiFeiStr = String.Format("贴皮：门 {0}*30={1} 双 {2}*30/6={3} 单 {4}*30/9={5} 合计:{6}"
                            , menCount, menCount * 30.0
                            , chuangTaoBanShuangShu, (chuangTaoBanShuangShu * (30.0 / 6)).ToString("f2")
                            , chuangTaoBanDanShu, (chuangTaoBanDanShu * (30.0 / 9)).ToString("f2")
                            , (menCount * 30.0 + chuangTaoBanShuangShu * (30.0 / 6) + chuangTaoBanDanShu * (30.0 / 9)).ToString("f2"));
                        //喷漆 110 90
                        dingDan.PenQiFeiStr = String.Format("油工：门 {0}*90={1} 双 {2}*90/6={3} 单 {4}*90/9={5}合计:{6}"
                            , menCount, menCount * 90.0
                            , chuangTaoBanShuangShu, (chuangTaoBanShuangShu * (90.0 / 6)).ToString("f2")
                            , chuangTaoBanDanShu, (chuangTaoBanDanShu * (90.0 / 9)).ToString("f2")
                            , (menCount * 90.0 + chuangTaoBanShuangShu * (90.0 / 6) + chuangTaoBanDanShu * (90.0 / 9)).ToString("f2"));

                        //纸皮 50
                        dingDan.BeiZhu = string.Format("喷漆每平50,套板平方:{0},喷窗套漆钱:{1},喷门{4},套板卖钱:{2},套板每平36.5料,料钱{3}"
                            , chuangTaoBanPing+menTaoBanPing, chuangTaoBanPing * 50,
                            chuangTaoBanDanMiShu * 50 / 1000 + chuangTaoBanShuangMiShu * 70 / 1000,
                            (menTaoBanPing + chuangTaoBanPing) * 36.5,
                        menCount * 230);

                    }
                }
            }

            DingDan.SetChooser(dingDans);
            DingDanContent.SetChooser(dingDanContents);
            LoadDingDanContentDataListView(dingDanContents, 1,true);
            LoadDingDanDataListView(dingDans, 1);
        }

        private void lvDingDanData_DoubleClick(object sender, EventArgs e)
        {
            DingDan dingDan = (DingDan)lvDingDanData.Items[lvDingDanData.SelectedItems[0].SubItems[0].Text].Tag;


            labelDingDanID.Text = dingDan.DingDanID;
            labelDingDanName.Text = dingDan.CustomName;
        }

        //算
        private void button5_Click(object sender, EventArgs e)
        {
            //Random r = new Random(DateTime.Now.Millisecond.GetHashCode());
            //foreach (float h in heights)
            //{
            //    r.Next(
            //}

            if (heights.Count > 0)
            {
                float huaDao = 0, _huaDao = 0;
                List<string> ss = new List<string>();
                huaDao = heights[0];
                ss.Add(heights[0].ToString());

                for (int i = 0; i < 1000; i++)
                {


                    //ss.Add(huaDao.ToString());

                    int j = GetRandom(heights.Count);


                    huaDao += heights[j];

                    if (huaDao < 6000)
                    {
                        ss.Add(heights[j].ToString());
                        _huaDao = huaDao;
                    }
                    else
                    {
                        randomInts.Remove(j);
                        huaDaos.Add(_huaDao);
                        string s = "";

                        foreach (string _s in ss)
                        {
                            s += _s + "  ";
                        }

                        s += (_huaDao.ToString());
                        ss.Clear();
                        huaDao = heights[0];
                        ss.Add(heights[0].ToString());
                        _huaDao = 0;
                        cbHuaDao.Items.Add(s);
                        //break;
                    }
                }
            }

        }

        //打印
        private void button7_Click(object sender, EventArgs e)
        {

            LoadDingDanContentDataListView(dingDanContents, 1, false);
            //  lvDingdanContentData.PrintHeaderString = String.Format("{0}{1}地址：{2}，{3}电话：{4}，顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶  ",
            //      dingDanSelected.CustomName,Environment.NewLine,dingDanSelected.CustomAddress,Environment.NewLine，dingDanSelected.CustomTelephone); 
            //lvDingdanContentData.PrintHeaderString = String.Format("{0}  {1} {2} {3}线{4}板{5}",
               // dingDanSelected.DingDanNo, dingDanSelected.CustomName, 
              //  dingDanSelected.DingDanPi, dingDanSelected.DingDanColor,
              //  dingDanSelected.TaoXianCount,dingDanSelected.TaoBanCount);
            lvDingdanContentData.PrintHeaderString = String.Format("{0} {1} {2} {3} 线{4}板{5}\r\n\r\n{6}\r\n{7}\r\n{8}",
                dingDanSelected.DingDanNo, dingDanSelected.CustomName,
                dingDanSelected.DingDanPi, dingDanSelected.DingDanColor,
                dingDanSelected.TaoXianCount, dingDanSelected.TaoBanCount,
                dingDanSelected.TiePiFeiStr,
                dingDanSelected.PenQiFeiStr,
                dingDanSelected.TaoXianSuan);
          //lvDingdanContentData.PrintTailString = String.Format("{0} \r\n 地址：{1}，电话：{2}，\r\n 顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶  ",
           //     dingDanSelected.CustomName, dingDanSelected.CustomAddress, dingDanSelected.CustomTelephone); 


            lvDingdanContentData.IsPreview = false;
            lvDingdanContentData.DoPrint();
            LoadDingDanContentDataListView(dingDanContents, 1, true);
        }

        //打印预览
        private void button8_Click(object sender, EventArgs e)
        {
            LoadDingDanContentDataListView(dingDanContents, 1, false);
            lvDingdanContentData.PrintHeaderString = String.Format("{0} {1} {2} {3} 线{4}板{5}\r\n\r\n{6}\r\n{7}\r\n{8}",
                dingDanSelected.DingDanNo, dingDanSelected.CustomName,
                dingDanSelected.DingDanPi, dingDanSelected.DingDanColor,
                dingDanSelected.TaoXianCount, dingDanSelected.TaoBanCount,
                dingDanSelected.TiePiFeiStr,
                dingDanSelected.PenQiFeiStr,
                dingDanSelected.TaoXianSuan);
          //lvDingdanContentData.PrintTailString = String.Format("{0} \r\n 地址：{1}，电话：{2}，\r\n 顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶  ",
          //     dingDanSelected.CustomName, dingDanSelected.CustomAddress, dingDanSelected.CustomTelephone); 

            lvDingdanContentData.IsPreview = true;
            lvDingdanContentData.DoPrint();
            LoadDingDanContentDataListView(dingDanContents, 1, true);
        }

        //订单内容选中行
        private void lvDingdanContentData_DoubleClick(object sender, EventArgs e)
        {
            DingDanContent dingDanContent = (DingDanContent)lvDingdanContentData.Items[lvDingdanContentData.SelectedItems[0].SubItems[0].Text].Tag;

            dingDanContentSelected = dingDanContent;

            labelDingDanID.Text = dingDanContent.DingDanID;

            cbCaiZhong.Text = dingDanContent.DingDanContentCaiZhong;
            cbDingDanType.Text = dingDanContent.DingDanContentType;
            cbXingHao.Text = dingDanContent.DingDanContentXingHao;
            tbCount.Text = dingDanContent.PassThickness.ToString();
            tbHeight.Text = dingDanContent.PassWidth.ToString();
            tbLength.Text = dingDanContent.PassHeight.ToString();
            tbDingDanContentName.Text = dingDanContent.DingDanContentName.ToString();

            if (heights.Count == 0)
            {
                heights.Add(dingDanContent.DingDanContentWeight);
                cbHeights.Items.Add(dingDanContent.DingDanContentWeight);
            }
            else
            {
                List<float> h = new List<float>();
                foreach (float f in heights)
                {
                    h.Add(f);
                }

                bool t = false;

                foreach (float f in h)
                {
                    if (f == dingDanContent.DingDanContentWeight)
                    {
                        t = true;
                    }
                }
                

                if (!t)
                {
                    heights.Add(dingDanContent.DingDanContentWeight);
                    cbHeights.Items.Add(dingDanContent.DingDanContentWeight);
 
                }
            }
            
            
            
        }

        //订单选中行
        private void lvDingDanData_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvDingDanData.SelectedItems.Count > 0)
            {
                DingDan dingDan = (DingDan)lvDingDanData.Items[lvDingDanData.SelectedItems[0].SubItems[0].Text].Tag;

                //主页显示
                tbDingDanNo.Text = dingDan.DingDanNo;
                tbPi.Text = dingDan.DingDanPi;
                tbColor.Text = dingDan.DingDanColor;
              
                tbCustomName.Text = dingDan.CustomName;
                tbCustomTel.Text  = dingDan.CustomTelephone;
                dtDing.Value = dingDan.DingDanComeDate;
                dtJiao.Value = dingDan.DingDanGoDate;     

                //附页显示
                labelDingDanID.Text = dingDan.DingDanID;
                labelDingDanName.Text = dingDan.CustomName;

                dingDanSelected = dingDan;

                LoadDingDanContentDataListView(dingDanContents, 1,true);
            }
            else
            {
                lvDingdanContentData.Clear();
            }
        }

        private void lvDingdanContentData_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvDingdanContentData.SelectedItems.Count > 0)
            {
                DingDanContent dingDanContent = (DingDanContent)lvDingdanContentData.Items[lvDingdanContentData.SelectedItems[0].SubItems[0].Text].Tag;

                if (dingDanContent.DingDanContentName == null)
                {
                    dingDanContent.DingDanContentName = "";
                }

                dingDanContentSelected = dingDanContent;

                labelDingDanID.Text = dingDanContent.DingDanID;

                cbCaiZhong.Text = dingDanContent.DingDanContentCaiZhong;
                cbDingDanType.Text = dingDanContent.DingDanContentType;
                cbXingHao.Text = dingDanContent.DingDanContentXingHao;
                tbCount.Text = dingDanContent.PassThickness.ToString();
                tbHeight.Text = dingDanContent.PassWidth.ToString();
                tbLength.Text = dingDanContent.PassHeight.ToString();
                tbDingDanContentName.Text = dingDanContent.DingDanContentName.ToString();

                //第一套板第二套板置为洞口尺寸
                cbTaoBanDiff.SelectedIndex = 0;
            }
        }

        private void lvDingdanContentData_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            orderBy = e.Column;
            LoadDingDanContentDataListView(dingDanContents, 1,true);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 加载订单数据
        /// </summary>
        /// <param name="_eventLogInfos">发送日志</param>
        void LoadDingDanDataListView(List<DingDan> _eventLogInfos, int imageIndex)
        {
            if (_eventLogInfos == null || _eventLogInfos.Count == 0 )
            {
                return;
            }
               

            lvDingDanData.Clear();

            //添加标头
            ColumnHeader title = new ColumnHeader();
            title.Text = "序号";
            title.Width = 40;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "编号";
            title.Width = 60;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "姓名";
            title.Width = 60;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "电话";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "颜色";
            title.Width = 100;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "门皮";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "套板个数";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "套线个数";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);


            title = new ColumnHeader();
            title.Text = "订货日期";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "交货日期";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "订单进度";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "套线计算";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "贴皮工费";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "油工工费";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "安门人";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "安门工费";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "套板单数";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);
            
            title = new ColumnHeader();
            title.Text = "套板双数";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "门个数";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            /* title = new ColumnHeader();
            title.Text = "备注";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "套板长度";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "套线长度";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "钱";
            title.Width = 70;
            this.lvDingDanData.Columns.Add(title);
            */
            string key;
            lvDingDanData.BeginUpdate();

            _eventLogInfos = _eventLogInfos.OrderBy(o => o.DingDanNo).ToList();

            try
            {
                foreach (DingDan eventLogInfo in _eventLogInfos)
                {
                    if (eventLogInfo.TiePiFeiStr == null)
                    {
                        eventLogInfo.TiePiFeiStr = "";
                    }
                    if (eventLogInfo.PenQiFeiStr == null)
                    {
                        eventLogInfo.PenQiFeiStr = "";
                    }

                    key = eventLogInfo.DingDanID.ToString();

                    lvDingDanData.Items.Add(key, key, imageIndex);

                    lvDingDanData.Items[key].Tag = eventLogInfo;

                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.DingDanNo.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.CustomName.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.CustomTelephone);
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.DingDanColor.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.DingDanPi.ToString());

                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TaoBanCount.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TaoXianCount.ToString());

                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.DingDanComeDate.ToShortDateString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.DingDanGoDate.ToShortDateString());

                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.DingDanProgress);
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TaoXianSuan);
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TiePiFeiStr.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.PenQiFeiStr.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.DingDanInstaller);
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.AnMenFei.ToString());

                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TaoBanDanCount.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TaoBanShuangCount.ToString());
                    lvDingDanData.Items[key].SubItems.Add(eventLogInfo.MenCount.ToString());
                    
                    //lvDingDanData.Items[key].SubItems.Add(eventLogInfo.BeiZhu.ToString());
                    //lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TaoBanMiSub.ToString());
                    //lvDingDanData.Items[key].SubItems.Add(eventLogInfo.TaoXianMiSub.ToString());
                    //lvDingDanData.Items[key].SubItems.Add(eventLogInfo.QianSub.ToString());
                   
                    lvDingDanData.Items[key].SubItems.Add(key);
                }
            }
            catch (InvalidOperationException)
            {
                lvDingDanData.EndUpdate();
                return;

            }



            //lvDingdanContentData.Items[1].SubItems[1].ForeColor = Color.Red;
            lvDingDanData.Items[0].ForeColor = Color.Red;
            lvDingDanData.Items[1].ForeColor = Color.Orange;
            lvDingDanData.Items[2].ForeColor = Color.Yellow;
            lvDingDanData.Items[3].ForeColor = Color.Green;
            lvDingDanData.Items[4].ForeColor = Color.Blue;
            lvDingDanData.Items[5].ForeColor = Color.Purple;//PaleGoldenrod

            lvDingDanData.Items[6].BackColor = Color.PaleGreen;
            lvDingDanData.Items[7].BackColor = Color.PaleTurquoise;
            lvDingDanData.Items[8].BackColor = Color.PaleGoldenrod;
            lvDingDanData.Items[9].BackColor = Color.PeachPuff;
            lvDingDanData.Items[10].BackColor = Color.PaleVioletRed;
            lvDingDanData.Items[11].BackColor = Color.Peru;
            

            lvDingDanData.EndUpdate();
            lvDingDanData.ContextMenuStrip = null;
        }

        /// <summary>
        /// 加载订单內容数据
        /// </summary>
        /// <param name="_eventLogInfos">发送日志</param>
        private void LoadDingDanContentDataListView(List<DingDanContent> __eventLogInfos, int imageIndex,
            Boolean isNotPint)
        {
            List<DingDanContent> _eventLogInfos = LoadDingDanContents(dingDanSelected);

            lvDingdanContentData.Clear();

            //添加标头
            ColumnHeader title = new ColumnHeader();

            title.Text = "编号";
            title.Width = 1;
            this.lvDingdanContentData.Columns.Add(title);

            if (isNotPint)
            {
                title = new ColumnHeader();
                title.Text = "名称";
                title.Width = 60;
                this.lvDingdanContentData.Columns.Add(title);
            }
            
            title = new ColumnHeader();
            title.Text = "类型";
            title.Width = 60;
            this.lvDingdanContentData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "型号";
            title.Width = 264;
            this.lvDingdanContentData.Columns.Add(title);

            int k = 61;

            title = new ColumnHeader();
            title.Text = "材种";
            title.Width = 50;
            this.lvDingdanContentData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "长1";
            title.Width = k;
            this.lvDingdanContentData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "宽1";
            title.Width = k;
            this.lvDingdanContentData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "数量1";
            title.Width = 50;
            this.lvDingdanContentData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "长2";
            title.Width = k;
            this.lvDingdanContentData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "宽2";
            title.Width = k;
            this.lvDingdanContentData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "数量2";
            title.Width = 50;
            this.lvDingdanContentData.Columns.Add(title);

            if (isNotPint == true)
            {
                title = new ColumnHeader();
                title.Text = "口高度";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);

                title = new ColumnHeader();
                title.Text = "口宽度";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);

                title = new ColumnHeader();
                title.Text = "口厚度";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);

                title = new ColumnHeader();
                title.Text = "套线1长度";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);

                title = new ColumnHeader();
                title.Text = "套线1个数";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);
                title = new ColumnHeader();
                title.Text = "套线2长度";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);

                title = new ColumnHeader();
                title.Text = "套线2个数";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);

                title = new ColumnHeader();
                title.Text = "平方";
                title.Width = 50;
                this.lvDingdanContentData.Columns.Add(title);

                title = new ColumnHeader();
                title.Text = "备注";
                title.Width = 180;
                this.lvDingdanContentData.Columns.Add(title);
            }

            if (_eventLogInfos == null || _eventLogInfos.Count == 0)
            {
                return;
            }

            string key;
            lvDingdanContentData.BeginUpdate();

            if (orderBy == 2)
            {
                _eventLogInfos = _eventLogInfos.OrderBy(o => o.DingDanContentType).ToList();
            }
            else if (orderBy == 8)
            {
                _eventLogInfos = _eventLogInfos.OrderBy(o => o.TaoXianLength).ToList();
            }
            else if (orderBy == 4)
            {//o.DingDanContentType
                _eventLogInfos = _eventLogInfos.OrderBy(o => o.DingDanContentCaiZhong  ).ToList();
            }
            else
            {
                _eventLogInfos = _eventLogInfos.OrderBy(o => o.CreateTime).ToList();
            }
            
            try
            {
                foreach (DingDanContent eventLogInfo in _eventLogInfos)
                {
                    key = eventLogInfo.DingDanContentID.ToString();
                    lvDingdanContentData.Items.Add(key, key, imageIndex);

                    lvDingdanContentData.Items[key].Tag = eventLogInfo;

                    if (eventLogInfo.DingDanContentName == null)
                    {
                        eventLogInfo.DingDanContentName = "";
                    }

                    if (isNotPint)
                    {
                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentName.ToString());
                    }

                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentType.ToString());
                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentXingHao);
                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentCaiZhong.ToString());
                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentLength.ToString());
                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentWeight.ToString());
                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentCount.ToString());

                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentLength1.ToString());
                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentWeight1.ToString());
                    lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.DingDanContentCount1.ToString());

                    if (isNotPint == true)
                    {
                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.PassHeight.ToString());
                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.PassWidth.ToString());
                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.PassThickness.ToString());

                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.TaoXianLength.ToString());
                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.TaoXianCount.ToString());
                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.TaoXianLength1.ToString());
                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.TaoXianCount1.ToString());

                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.TaoBanPing.ToString());

                        if (eventLogInfo.BeiZhu == null)
                        {
                            eventLogInfo.BeiZhu = " ";
                        }

                        lvDingdanContentData.Items[key].SubItems.Add(eventLogInfo.BeiZhu.ToString());
                    }
                    lvDingdanContentData.Items[key].SubItems.Add(key);
                }
            }
            catch (InvalidOperationException)
            {
                lvDingdanContentData.EndUpdate();
                return;

            }

            lvDingdanContentData.EndUpdate();
            lvDingdanContentData.ContextMenuStrip = null;
        }

        List<DingDanContent> LoadDingDanContents(DingDan dingDan)
        {
            List<DingDanContent> _dingDanContents = new List<DingDanContent>();

            if (dingDanContents == null)
                return _dingDanContents;

            if (dingDanContents.Count > 0)
            {
                foreach (DingDanContent dingDanContent in dingDanContents)
                {
                    if (dingDanContent.DingDanID == dingDan.DingDanID)
                    {
                        _dingDanContents.Add(dingDanContent);
                    }
                }
            }

            return _dingDanContents;
        }

        //产生随机数
        private int GetRandom(int zhongzi)
        {
            int r = 0;

            if (randomInts.Count == zhongzi)
            {
                randomInts.Clear();
            }

        hao: Random random = new Random(DateTime.Now.Millisecond.GetHashCode());
            r = random.Next(1, zhongzi);
            if (randomInts.Count > 0)
            {

                //for (int i = 0; i < 100; i++)
                //{
                //    bool b = false;
                //    foreach (int _r in randomInts)
                //    {
                //        if (r == _r)
                //        {
                //            b = true;
                //            break;
                //        }
                //    }
                //    if (b == false)
                //    {

                //        break;
                //    }
                //}

                if (randomInts.Contains(r))
                {
                    if (randomInts.Count < zhongzi - 1)
                    {
                        goto hao;
                    }
                    else
                    {
                        randomInts.Clear();
                        goto hao;
                    }
                }
                else
                {
                    randomInts.Add(r);
                    return r;
                }
            }
            else
            {
                randomInts.Add(r);
                return r;
            }

        }

        #endregion

        private void cbTaoBanDiff_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTaoBanDiff.Text == "洞口尺寸")
            {
                tbLength.Text = dingDanContentSelected.PassHeight.ToString();
                tbHeight.Text = dingDanContentSelected.PassWidth.ToString();
                tbCount.Text = dingDanContentSelected.PassThickness.ToString();

            }
            else if (cbTaoBanDiff.Text == "第一套板")
            {
                tbLength.Text = dingDanContentSelected.DingDanContentLength.ToString();
                tbHeight.Text = dingDanContentSelected.DingDanContentWeight.ToString();
                tbCount.Text = dingDanContentSelected.DingDanContentCount.ToString();
            }
            else if (cbTaoBanDiff.Text == "第二套板")
            {
                tbLength.Text = dingDanContentSelected.DingDanContentLength1.ToString();
                tbHeight.Text = dingDanContentSelected.DingDanContentWeight1.ToString();
                tbCount.Text = dingDanContentSelected.DingDanContentCount1.ToString();
            }
        }
    }
}
